package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author mk177
 */
@Component
@Slf4j
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) {
//        System.out.println("spring 消费者接收到消息：【" + msg + "】");
//    }

    /**
     * 测试二 工作队列
     */
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueueMessage(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到消息：【" + msg + "】");
        log.info("消费者1正在处理消息......");
        Thread.sleep(100);
    }
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueueMessage2(String msg) throws InterruptedException {
//        System.out.println("消费者2接收到消息：【" + msg + "】");
        log.error("消费者2正在处理消息...........");
        Thread.sleep(300);

    }


    /**
     * 测试三 广播模式
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueueMessage(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到消息：【" + msg + "】");
        log.info("消费者1正在处理消息......");

    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueueMessage2(String msg) throws InterruptedException {
//        System.out.println("消费者2接收到消息：【" + msg + "】");
        log.error("消费者2正在处理消息...........");


    }

    /**
     * 测试四 路由模式
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.exchange",
                    type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void testDirectQueue(String msg) throws InterruptedException {
       log.info("消费者1正在处理消息......"+msg);

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.exchange",
                    type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void testDirectQueue2(String msg) throws InterruptedException {
       log.error("消费者2正在处理消息......"+msg);

    }



    /**
     * 测试五 通配符模式
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者接收到topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者接收到topic.queue2的消息：【" + msg + "】");
    }


    @RabbitListener(queues = "Object.queue")
    public void listenObjectMessage(Object msg) {
        System.out.println("spring 消费者接收到消息：【" + msg + "】");
    }


}